# program that simplifies fractions

printNumber: # pops and prints a number (0 - 99)
  $0 10 > ?
    $0 10 / "0" + ->
  .

  10 % "0" + ->
.

_simplify:   # recursively simplifies fraction, in: nom, denom, divisor
  $0 $2 <= $1 $4 <= && ?          # divisor not greater than nom and denom?
    $1 $1 % 0 = $3 $2 % 0 = && ?  # divisor divides nom or denom?
      $2 $1 / $:3                 # divide both nom and denom
      $1 $1 / $:2
    ;
      ++                          # try next divisor
    .
 
    _simplify
  .
.

simplifyFraction: 2 _simplify ^ .
printFraction: $1 printNumber "/" -> $0 printNumber .
printSimplified: printFraction 0 " = " --> simplifyFraction printFraction 10 -> .

# main program starts here:

8 4   printSimplified
55 22 printSimplified
49 14 printSimplified
